from __future__ import annotations

import argparse
import subprocess
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parent


def _run(cmd: list[str]) -> None:
    print("+", " ".join(cmd))
    subprocess.run(cmd, cwd=ROOT, check=True)


def _require_exists(path: Path, hint: str) -> None:
    if not path.exists():
        raise SystemExit(f"Missing required input: {path}\n\n{hint}\n")


def main() -> int:
    ap = argparse.ArgumentParser(description="Run the full replication pipeline.")
    ap.add_argument("--skip-ess", action="store_true", help="Skip building ESS parquet (requires outputs/ess_r8_r11_min.parquet).")
    ap.add_argument("--skip-external", action="store_true", help="Skip Eurostat fetch/build steps if outputs already exist.")
    ap.add_argument(
        "--skip-randomization",
        action="store_true",
        help="Skip timing-permutation randomization inference figure (requires permutation outputs).",
    )
    args = ap.parse_args()

    ess_hint = (
        "Download ESS integrated files from https://www.europeansocialsurvey.org/ and place zips under "
        "`ess_data/integrated_files/`, plus `ess_data/integrated_files_coverage.csv`."
    )

    if args.skip_ess:
        _require_exists(ROOT / "outputs" / "ess_r8_r11_min.parquet", ess_hint)
    else:
        _require_exists(ROOT / "ess_data" / "integrated_files_coverage.csv", ess_hint)
        _require_exists(
            ROOT / "ess_data" / "integrated_files",
            "Create `ess_data/integrated_files/` and place `ESS8/9/10/11_integrated_*.zip` inside it.",
        )

    # 1) Build minimal ESS dataset
    if not args.skip_ess:
        _run([sys.executable, "analysis/02_build_ess_min_dataset_r8_r11.py"])

    # 2) External data (Eurostat) + construct rollout shift-share
    if not args.skip_external:
        _run([sys.executable, "analysis/07_fetch_eurostat_isoc_r_broad_h.py"])
        _run([sys.executable, "analysis/17_fetch_eurostat_country_year_isoc_cbs.py"])
        _run([sys.executable, "analysis/20_fetch_eurostat_country_year_isoc_cbt.py"])
        _run([sys.executable, "analysis/08_build_broadband_region_year_eurostat.py"])
        _run([sys.executable, "analysis/18_build_shiftshare_rollout_region_year.py"])

    # 3) Main identification + tables/figures for the manuscript
    _run([sys.executable, "analysis/19_iv_shiftshare_rollout_absorb.py"])
    _run([sys.executable, "analysis/21_compare_ols_2sls_liml_shiftshare.py"])
    _run([sys.executable, "analysis/22_export_tables_to_latex.py"])
    _run([sys.executable, "analysis/23_make_joc_figures.py"])
    perm = ROOT / "outputs" / "iv_shiftshare_randomization_firststage.csv"
    if args.skip_randomization or not perm.exists():
        print(f"Skip randomization figure (missing or disabled): {perm}")
    else:
        _run([sys.executable, "analysis/30_make_randomization_inference_figure.py"])

    print("Replication pipeline completed.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
